package com.apes.fn.server.vehicleFile.model;

import com.apes.fn.server.transferVehicle.model.InnerPickDeliveryUpCar;
import com.apes.fn.server.transferVehicle.model.InnerPickDeliveryUpCarItem;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: apes-server
 * @description: 车辆档案区域 (省份)
 * @author: czs
 * @create: 2023-11-02 11:25
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VehicleFileArea.CONST_TABLE_NAME)
public class VehicleFileArea  extends Model {
    public static final String CONST_TABLE_NAME = "fn_vehicle_file_area";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "车辆档案区域编码", padding = 12)
    private String id;

    @Column(name = "name", length = 32,nullable = false)
    @Comment("名称")
    private String name;

    @Column(name = "api_type", length = 32,nullable = false)
    @Comment("接口类型")
    private String apiType;

    @OneToMany(mappedBy = "vehicleFileArea", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    private List<VehicleFileAreaItem> vehicleFileAreaItems = new ArrayList<>(); //明细

    public List<VehicleFileAreaItem> getVehicleFileAreaItems() {
        for (VehicleFileAreaItem item : vehicleFileAreaItems) {
            item.setVehicleFileArea(this);
        }
        return vehicleFileAreaItems;
    }

    public VehicleFileAreaItem findItemByApiType(String type){
        VehicleFileAreaItem vehicleFileAreaItem1 = this.getVehicleFileAreaItems().stream().filter(vehicleFileAreaItem -> type.equals(vehicleFileAreaItem.getApiType())).findFirst().orElse(null);
        if (vehicleFileAreaItem1==null)throw new RuntimeException("未找到对应类型接口:"+type);
        return vehicleFileAreaItem1;
    }

    @Comment("删除状态")
    @Column(name = "is_delete", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    private boolean deleted = false;
}
